package net.dntuan.training.mvc.manager.impl;

import java.util.List;

import net.dntuan.training.mvc.dao.CityDao;
import net.dntuan.training.mvc.domain.City;
import net.dntuan.training.mvc.manager.CityManager;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

@Component("cityManager")
@Transactional(readOnly = false)
public class CityManagerImpl implements CityManager {

    @Autowired
    private CityDao cityDao;

    @Override
    @Transactional(readOnly = true)
    public City findOne(Long id) {
        return cityDao.findOne(id);
    }

    // TODO why does read-operation require a transaction?
    @Override
    @Transactional(readOnly = true)
    public List<City> findAll() {
        return cityDao.findAll();
    }

    @Override
    @Transactional(readOnly = true)
    public List<City> search(String cityName, String country, Integer minPop, Integer maxPop) {
        return cityDao.search(cityName, country, minPop, maxPop);
    }

    @Override
    public void save(City city) {
        cityDao.save(city);
    }

    @Override
    public void update(City city) {
        cityDao.update(city);
    }

    @Override
    public void delete(Long id) {
        City city = cityDao.findOne(id);
        cityDao.delete(city);
    }

}
